package com.coolgor.axnut.web.mdmis;

import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.axnut.util.HttpServletRequestUtil;
import com.coolgor.axnut.dto.mdmis.DeliverInfoExecution;
import com.coolgor.axnut.dto.mdmis.PolicyExecution;
import com.coolgor.axnut.entity.mdmis.DeliverInfo;
import com.coolgor.axnut.entity.mdmis.Policy;
import com.coolgor.axnut.enums.mdmis.PolicyOperationStateEnum;
import com.coolgor.axnut.service.mdmis.DeliverInfoService;
import com.coolgor.axnut.service.mdmis.PolicyMonitorService;
import com.coolgor.axnut.service.mdmis.PolicyService;


@Controller
@RequestMapping(value="mdmis/PolicyDeliverInfo", method={RequestMethod.GET,RequestMethod.POST})
public class PolicyDeliverInfoController {
	@Autowired
	private  PolicyService policyService;
	@Autowired
	private  PolicyMonitorService policyMonitorService;
	@Autowired
	private  DeliverInfoService deliverInfoService;	

	
	
	@RequestMapping(value = "/getPolicyInfo", method={RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getPolicyInfo(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    Policy policy = null;
	    String insureNo = HttpServletRequestUtil.getString(request, "insure_no");
	    if (!"".equals(insureNo) && insureNo!=null) {
	    	policy = policyService.getPolicy(insureNo);
			if (null != policy ) {
				try {
					modelMap.put("data",policy);
					modelMap.put("success", true);
				} catch (Exception e) {
					modelMap.put("success", false);
					modelMap.put("errMsg", e.toString());
				}
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", "Empty insure information!");
			}
	    } else {
	    	modelMap.put("success", false);
			modelMap.put("errMsg", "Empty insure NO");
	    }	    
		return modelMap;
	}	
	
	@RequestMapping(value = "/getPolicyList", method={RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getPolicyList(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    int page = HttpServletRequestUtil.getInt(request, "page");
	    int pageSize = HttpServletRequestUtil.getInt(request, "rows");
	    Policy policyCondition = new Policy();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String insureNo = HttpServletRequestUtil.getString(request, "insureNo");
	    if (!"".equals(insureNo) && insureNo!=null) {
	    	policyCondition.setInsureNo(insureNo);
	    }
	    String application = HttpServletRequestUtil.getString(request, "application");
	    if (!"".equals(application) && application!=null) {
	    	policyCondition.setApplicant(application);
	    }
	    String mobilephone = HttpServletRequestUtil.getString(request, "mobilephone");
	    if (!"".equals(mobilephone) && mobilephone!=null) {
	    	policyCondition.setMobilephone(mobilephone);
	    }
	    String insureTime = HttpServletRequestUtil.getString(request, "insureTime");
	    if (!"".equals(insureTime) && insureTime!=null) {
	    	try {
				policyCondition.setInsureTime(sdf.parse(insureTime));
			} catch (ParseException e) {
				e.printStackTrace();
			}
	    }
	    PolicyExecution exe1 = policyService.getPolicyList(policyCondition, page, pageSize);
	    List<Policy> list = new ArrayList<Policy>();
	    int count = 0;
	    if (null != exe1 ) {
	    	list.addAll(exe1.getList());
	    	count = exe1.getCount();
	    }
	    
	    String deliverNo = HttpServletRequestUtil.getString(request, "deliverNo");
	    if (!"".equals(deliverNo) && deliverNo!=null) {
	    	DeliverInfo deliverInfoCondition = new  DeliverInfo();
	    	deliverInfoCondition.setDeliverNo(deliverNo);
	    	deliverInfoCondition.setReturnNo(deliverNo);
		    DeliverInfoExecution exe2 = deliverInfoService.getDeliverInfoListFuzzy(deliverInfoCondition, page, pageSize);
		    if (null != exe2 ) {
		    	List<String> insureNos = new ArrayList<String>();
		    	for (int i=0; i<exe2.getCount(); i++) {
		    		insureNos.add(exe2.getList().get(i).getInsureNo());
		    	}
		    	exe1 = null ;
			    exe1 = policyService.getPolicyList(insureNos, page, pageSize);
			    if (exe1.getState()==PolicyOperationStateEnum.SUCCESS.getState()) {
			    	list.clear();
			    	list.addAll(exe1.getList());
			    	count = exe1.getCount();			    	
			    }
//			    下面这段代码 当时出错了 exe1.getList()出现了null
//			    if (null != exe1 ) {
//			    	list.clear();
//			    	list.addAll(exe1.getList());
//			    	count = exe1.getCount();
//			    }
//			 上面这段代码 当时出错了   
		    }
	    }	    
		if (list.size()>0 ) {
			try {
				modelMap.put("total",count);
				modelMap.put("rows",list);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty List");
		}
		return modelMap;
	}
	
}
